import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zyj
 * @date 2022年05月31日 11:04
 */
public class ReadExcel {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        try {
            Workbook book = new XSSFWorkbook("student.xlsx");
            Sheet sheet = book.getSheetAt(0);
            System.out.println("起始行标:" + sheet.getFirstRowNum());
            System.out.println("有效行数：" + sheet.getPhysicalNumberOfRows());
            int first = sheet.getFirstRowNum();
            int last = sheet.getLastRowNum();

            System.out.println("最后一行：" + last);
            for (int i = first+1; i <= last; i++) {
                Row row = sheet.getRow(i);


                Student student = new Student();
                int firstCell = row.getFirstCellNum();
                Cell cell = row.getCell(firstCell++);
                student.setId(Integer.valueOf(cell.getStringCellValue()));
                Cell name = row.getCell(firstCell++);
                student.setName(name.getStringCellValue());

                Cell age = row.getCell(firstCell++);
                student.setAge(Integer.valueOf(age.getStringCellValue()));

                students.add(student);
            }

            students.forEach(student -> {
                System.out.println(student);
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
